<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #box {
            width: 620px;
            margin: 160px auto;
        }

        #box .searchbox {
            overflow: hidden;
            border-radius: 5px;
            border: 1px solid #dcdcdc;
        }

        #box .searchbox input {
            float: left;
            border: none;
            outline: none;
            height: 40px;
        }

        #box .searchbox input[type='text'] {
            width: 480px;
            padding-left: 8px;
        }

        #box .searchbox input[type='button'] {
            width: 130px;
            color: white;
            border-radius: 0 5px 5px 0;
            background: #4e6ef2;
        }

        .list {
            border: 1px solid #dcdcdc;
            border-top: none;
        }

        .list li {
            list-style: none;
            line-height: 30px;
        }

        .list .active {
            background-color: pink;
        }
    </style>
</head>

<body>
    <div id="box">
        <div class="searchbox">
            <input type="text" placeholder="请输入要搜索的内容" v-model='msg' @keydown.up.prevent="up()" @keydown.down="down()">
            <input @click="search()" type="button" value="百度一下">
            <ul id="list" class="list">
                <li v-for='(item,index) in arr' :class='{active:index==nowIndex}'>
                    {{item}}
                </li>
                <!-- 
                所有的li数据，都有自己的index
                我们可以定义一个nowIndex来记录当前被选中的下标
                比较index和nowIndex的关系
                如果index==nowIndex,那么该条数据是被选中的数据，也就是说背景变为灰色
             -->
            </ul>
        </div>

    </div>
    <script src="./js/vue.js"></script>
    <script src="./js/vue-resource.js"></script>
    <script>
        let vm = new Vue({
            el: "#box",
            data: {
                arr: [],
                msg: "",
                nowIndex: 1  // 当前用户选中的索引
            },
            methods: {
                search() {
                    this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?', {
                        // wd=iphone&jsonp=cb
                        params: {
                            wd: this.msg
                        },
                        jsonp: "cb"
                    }).then(res => {
                        console.log(res);
                        this.arr = res.data.s; // 请求数据
                    }).catch(err => {
                        console.log(err);
                    })
                },
                // 按的是上键
                up() {
                    console.log("up");
                    this.nowIndex--;
                    if (this.nowIndex < 0) {
                        this.nowIndex = this.arr.length - 1;
                    }
                },
                // 按的是下键
                down() {
                    console.log("down");
                    // 改变index索引 ++
                    this.nowIndex++;
                    if (this.nowIndex > this.arr.length - 1) {
                        this.nowIndex = 0;
                    }
                }
            }
        })
    </script>
</body>

</html>